<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: jz
 * Date: 2016/8/23
 * Time: 15:02
 */
class Node_model extends CI_Model{
    private $error = '';
    
    public function find_opened_node($data = array())
    {
        $user = $this->session->userdata('uid');
        
        if (isset($data['tid'])){
            $where = array(
                'uid' => $user,
                'sid' => $data['sid'],
                'tid' => $data['tid']
            );
        }else {
            $where = array(
                'uid' => $user,
                'sid' => $data['sid'],
                'tid' => null
            );
        }
        

        $result = $this->db->select('*')->
        from('opened_node')->
        where($where)->
        get()->result_array();
        if (!$result){
            $this->error = '获取失败';
            return false;
        }
        return $result;
    }

    public function insert($node = array())
    {
         if (!isset($node['sid'])){
             return false;
         }
        $in['sid'] = $node['sid'];
        
        if (!isset($node['ip'])){
            return false;
        }
        $in['ip'] = $node['ip'];
        
        if (!isset($node['port'])){
            return false;
        }
        $in['port'] = $node['port'];
        
        if (isset($node['tid'])){
            $in['tid'] = $node['tid'];
            $in['usetid'] = true;
        }else{
            $in['usetid'] = false;
        }

        $in['uid'] = $this->session->userdata('uid');
        
        $result = $this->db->insert('opened_node', $in);
        if (!$result || $this->db->affected_rows() == 0){
            $this->error = '添加失败';
            return false;
        }
        return true;
    }
    
    public function delete($port = '')
    {
        if ($port == ''){
            return false;
        }

        $result = $this->db->delete('opened_node', array('port' => $port));
        if (!$result || $this->db->affected_rows() == 0){
            return false;
        }
        return true;
    }
    
    
    
    public function display_errors()
    {
        return $this->error;
    }
}